package fragmentparent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.R;


/**
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class MenuBelowFragment extends Fragment {

	private View rootView,liner1, liner2, liner3, liner4;
	public Fragment fragment1, fragment2, fragment3, fragment4,mContentFragment;
	private FragmentManager fragmentManager;
	private TextView item1, item2, item3, item4, titem1, titem2, titem3,titem4;
	private static MenuBelowFragment menuBelowFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		menuBelowFragment = this;
		Log.v("Tag", "onCreate");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.activity_fragment_menu, container, false);
		initView();
		fragmentManager = getActivity().getSupportFragmentManager();
		initItemFragment();
		Log.v("Tag", "onCreateView");
		return rootView;
	}

	public static MenuBelowFragment newInstance() {
		if (menuBelowFragment == null)
			menuBelowFragment = new MenuBelowFragment();
		return menuBelowFragment;
	}

	private void initItemFragment() {
		// TODO Auto-generated method stub
		fragment1 = Fragment_A.newInstance();
		switchContent(null, fragment1);
	}

	private void initView() {
		// TODO Auto-generated method stub
		liner1 = rootView.findViewById(R.id.liner1);
		liner2 = rootView.findViewById(R.id.liner2);
		liner3 = rootView.findViewById(R.id.liner3);
		liner4 = rootView.findViewById(R.id.liner4);
		item1 = (TextView) rootView.findViewById(R.id.item1);
		item2 = (TextView) rootView.findViewById(R.id.item2);
		item3 = (TextView) rootView.findViewById(R.id.item3);
		item4 = (TextView) rootView.findViewById(R.id.item4);
		titem1 = (TextView) rootView.findViewById(R.id.titem1);
		titem2 = (TextView) rootView.findViewById(R.id.titem2);
		titem3 = (TextView) rootView.findViewById(R.id.titem3);
		titem4 = (TextView) rootView.findViewById(R.id.titem4);
		liner1.setOnClickListener(itemListener);
		liner2.setOnClickListener(itemListener);
		liner3.setOnClickListener(itemListener);
		liner4.setOnClickListener(itemListener);
	}

	public OnClickListener itemListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.liner1:
				refreshView();
				if (fragment1 == null) {
					fragment1 = Fragment_A.newInstance();
				}else{
					//刷新数据用
					((Fragment_A)fragment1).initData();
				}
				item1.setBackgroundResource(R.mipmap.btn1_cur);
				titem1.setTextColor(getResources().getColor(R.color.iscur));
				switchContent(mContentFragment, fragment1);
				break;

			case R.id.liner2:
				refreshView();
				/*if (fragment2 == null) {
					fragment2 = Fragment_B.newInstance();
				}else{
					//刷新数据用
					//((Fragment_C)fragment2).initData();
				}*/
				fragment2 = Fragment_B.newInstance();
				item2.setBackgroundResource(R.mipmap.btn2_cur);
				titem2.setTextColor(getResources().getColor(R.color.iscur));
				switchContent(mContentFragment, fragment2);
				break;
			case R.id.liner3:
				Toast.makeText(getActivity(),"开发中",Toast.LENGTH_LONG).show();
				/*refreshView();
				if (fragment3 == null) {
					fragment3 = Fragment_C.newInstance();
				}else{
					//刷新数据用
					//((Fragment_C)fragment3).initData();
				}
				item3.setBackgroundResource(R.mipmap.btn3_cur);
				titem3.setTextColor(getResources().getColor(R.color.iscur));
				switchContent(mContentFragment, fragment3);*/
				break;
			case R.id.liner4:
				refreshView();
				if (fragment4 == null) {
					fragment4 = Fragment_D.newInstance();
				}else{
					//刷新数据用
					((Fragment_D)fragment4).initData();
				}
				item4.setBackgroundResource(R.mipmap.btn4_cur);
				titem4.setTextColor(getResources().getColor(R.color.iscur));
				switchContent(mContentFragment, fragment4);
				break;
			default:
				break;
			}
		}

	};
	
	private void refreshView() {
		// TODO Auto-generated method stub
		item1.setBackgroundResource(R.mipmap.btn1);
		item2.setBackgroundResource(R.mipmap.btn2);
		item3.setBackgroundResource(R.mipmap.btn3);
		item4.setBackgroundResource(R.mipmap.btn4);
		titem1.setTextColor(getResources().getColor(R.color.textcolor3));
		titem2.setTextColor(getResources().getColor(R.color.textcolor3));
		titem3.setTextColor(getResources().getColor(R.color.textcolor3));
		titem4.setTextColor(getResources().getColor(R.color.textcolor3));
	}

	public void switchContent(Fragment from, Fragment to) {
		if (mContentFragment != to) {
			mContentFragment = to;
			FragmentTransaction transaction = fragmentManager
					.beginTransaction();
			if (from == null) {
				transaction.add(R.id.contentfragment, to).commit();
				return;
			}
			if (!to.isAdded()) {
				Log.v("Tag", "transaction.hide(from).add(R.id.mainFragments, to).commit()");
				transaction.hide(from).add(R.id.contentfragment, to).commit();
			} else {
				Log.v("Tag", "transaction.hide(from).show(to).commit()");
				transaction.hide(from).show(to).commit();
			}
			// transaction.replace(R.id.contentfragment, to).commit();
		}
	}

}
